//
// Created by 11510 on 2024/7/4.
//

#include "TaskQueue.h"

TaskQueue::TaskQueue(size_t queueSize) :
        _queueSize(queueSize), _queue(), _mutex(),
        _notFull(), _notEmpty() {

}

TaskQueue::~TaskQueue() {
}

void TaskQueue::push(const int &value) {
    unique_lock<mutex> uniqueLock(_mutex);
    while (full()) {
        _notFull.wait(uniqueLock);
    }
    _queue.push(value);

    _notEmpty.notify_all();
}

int TaskQueue::pop() {
    unique_lock<mutex> uniqueLock(_mutex);
    while (empty()) {
        _notEmpty.wait(uniqueLock);
    }
    int tmp = _queue.front();
    _queue.pop();

    _notFull.notify_all();
    return tmp;
}

bool TaskQueue::full() const {
    return _queueSize == _queue.size();
}

bool TaskQueue::empty() const {
    return 0 == _queue.size();
}
